﻿using System;
using Microsoft.VisualStudio.SharePoint.Explorer;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint.Explorer.Extensions;
using Microsoft.VisualStudio.SharePoint;
using CodeCounsel.SharePoint.DevTools.Commands;
using Scope = CodeCounsel.SharePoint.DevTools.Commands.FeatureScope;
using System.Windows.Forms;

namespace CodeCounsel.SharePoint.DevTools.ExplorerNodeExtensions
{
    [Export(typeof(IExplorerNodeTypeExtension))]
    [ExplorerNodeType(ExtensionNodeTypes.FeatureNode)]
    public class FeatureNodeExtension
        : IExplorerNodeTypeExtension
    {
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeChildrenRequested += NodeType_NodeChildrenRequested;
            nodeType.NodeMenuItemsRequested += NodeType_NodeMenuItemsRequested;
        }

        void NodeType_NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            GenericFolderNode.Create(e.Node, "Dependencies", new Action<IExplorerNode>(FeatureDependencyNodeTypeProvider.CreateFeatureDependencyNodes));
            GenericFolderNode.Create(e.Node, "Elements", new Action<IExplorerNode>(FeatureElementNodeTypeProvider.CreateFeatureElementNodes));
        }

        void NodeType_NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            IFeatureNodeInfo info = e.Node.GetAnnotation<IFeatureNodeInfo>();
            FeatureInfo featureDetails = new FeatureInfo() 
            { 
                FeatureID = info.Id, 
                Scope = (Scope)info.Scope
            };
            AddToggleFeatureMenuItem(e.Node, e.MenuItems, featureDetails);
            AddCopyIDMenuItem(e.Node, e.MenuItems, info);
        }

        void AddCopyIDMenuItem(IExplorerNode parent, IMenuItemCollection items, IFeatureNodeInfo nodeInfo)
        {
            IMenuItem item = items.Add("Copy ID");
            item.Click +=
                delegate
                {
                    Clipboard.SetText(nodeInfo.Id.ToString("D"));
                };
        }

        void AddToggleFeatureMenuItem(IExplorerNode parent, IMenuItemCollection items, FeatureInfo featureDetails)
        {
            bool isEnabled = parent.Context.SharePointConnection.ExecuteCommand<FeatureInfo, bool>(CommandIDs.IsFeatureEnabled, featureDetails);
            if (isEnabled == false)
            {
                IMenuItem item = items.Add(
                    String.Format("Activate on {0}", featureDetails.Scope));
                item.Click +=
                    delegate
                    {
                        try
                        {
                            Cursor.Current = Cursors.WaitCursor;
                            parent.Context.SharePointConnection.ExecuteCommand<FeatureInfo>(
                                CommandIDs.EnableFeature, featureDetails);
                        }
                        finally
                        {
                            Cursor.Current = Cursors.Default;
                        }
                    };
            }
            else
            {
                IMenuItem item = items.Add(
                    String.Format("Deactivate from {0}", featureDetails.Scope));
                item.Click +=
                    delegate
                    {
                        try
                        {
                            Cursor.Current = Cursors.WaitCursor;
                            parent.Context.SharePointConnection.ExecuteCommand<FeatureInfo>(
                                CommandIDs.DisableFeature, featureDetails);
                        }
                        finally
                        {
                            Cursor.Current = Cursors.Default;
                        }
                    };
            }
        }
    }
}
